package cn.ms.doctor.Service;

import cn.ms.doctor.Entity.Dept;
import cn.ms.doctor.Mapper.DeptMapper;
import cn.ms.doctor.Mapper.UserMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DeptServiceImpl implements DeptService {
    @Resource
    DeptMapper deptMapper;
    @Resource
    UserMapper userMapper;

    @Override
    public List<Dept> getDepts() {
        return deptMapper.getDepts();
    }

    @Override
    public Map<String, Object> getDeptsAndUserCount(Map<String, Object> params) {
        Map<String, Object> data=new HashMap<>();
        Page<Object> objects = PageHelper.startPage((Integer) params.get("pageIndex"), (Integer) params.get("pageSize"));
        List<Map<String,Object>> dataDepts=deptMapper.getDeptsBySearchAndPageIndex(params);
        for (Map<String, Object> dataDept : dataDepts) {
            dataDept.put("userCount",userMapper.getUserCountByDeptId((Integer) dataDept.get("deptId")));
        }
        data.put("pageData",dataDepts);
        data.put("pageIndex",objects.getPageNum());
        data.put("pageSize",objects.getPageSize());
        data.put("pageCount",objects.getPages());
        return data;
    }

    @Override
    public boolean updateDeptNameByDeptId(Dept params) {
        return deptMapper.updateDeptNameByDeptId(params);
    }

    @Override
    public boolean addDept(Dept dept) {
        return deptMapper.addDept(dept);
    }

    @Override
    public boolean deleteDeptByDeptId(Integer deptId) {
        if (userMapper.getUserCountByDeptId(deptId)!=0) return false;
        return deptMapper.deleteDeptByDeptId(deptId);
    }
}
